package com.acme.saul;

import com.acme.entity.Carretera;
import com.acme.entity.Servicio;
import com.acme.entity.Tramo;
import com.acme.entity.util.DBManager;
import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.JTextComponent;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.xswingx.PromptSupport;

public class PanelCarreteraa extends javax.swing.JPanel implements ChangeListener {

    /** Creates new form PanelCarreteraa */
    public PanelCarreteraa() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        bean = new com.acme.entity.Carretera();
        entityManager1 = DBManager.getEntityManager();
        query_tramo = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT t FROM Tramo t");
        listaTramos = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : query_tramo.getResultList();
        query_tabla = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT t FROM Carretera t");
        lista_tabla = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : query_tabla.getResultList();
        query_servicio = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT t FROM Servicio t");
        lista_servicio = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : query_servicio.getResultList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField_ruta = new javax.swing.JTextField();
        jTextField_velocidad = new javax.swing.JTextField();
        jTextField_numcarriles = new javax.swing.JTextField();
        jTextField_tipocarretera = new javax.swing.JTextField();
        jComboBox_tramo = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList_servicio = new javax.swing.JList();

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, lista_tabla, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${ruta}"));
        columnBinding.setColumnName("Ruta");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${velocidad}"));
        columnBinding.setColumnName("Velocidad");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${numcarriles}"));
        columnBinding.setColumnName("Numcarriles");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tipoCarretera}"));
        columnBinding.setColumnName("Tipo Carretera");
        columnBinding.setColumnClass(com.acme.entity.TipoCarretera.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${servicioList}"));
        columnBinding.setColumnName("Servicio List");
        columnBinding.setColumnClass(java.util.List.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tramoList}"));
        columnBinding.setColumnName("Tramo List");
        columnBinding.setColumnClass(java.util.List.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();

        jScrollPane1.setViewportView(jTable1);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton1.setText("AGREGAR");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton2.setText("ELIMINAR");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${ruta}"), jTextField_ruta, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${velocidad}"), jTextField_velocidad, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${numcarriles}"), jTextField_numcarriles, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, bean, org.jdesktop.beansbinding.ELProperty.create("${tipoCarretera}"), jTextField_tipocarretera, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaTramos, jComboBox_tramo);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.tramoList}"), jComboBox_tramo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jComboBox_tramo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_tramoActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, lista_servicio, jList_servicio);
        bindingGroup.addBinding(jListBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.servicioList}"), jList_servicio, org.jdesktop.beansbinding.BeanProperty.create("selectedElements"));
        bindingGroup.addBinding(binding);

        jScrollPane2.setViewportView(jList_servicio);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox_tramo, 0, 360, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField_ruta, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                .addComponent(jTextField_velocidad)
                                .addComponent(jTextField_numcarriles))
                            .addComponent(jTextField_tipocarretera, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField_ruta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField_velocidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField_numcarriles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField_tipocarretera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBox_tramo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    
        Carretera nuevo = new Carretera();
        nuevo.addChangeListener(this);
        bindingGroup.unbind();
        lista_tabla.add(nuevo);
        bindingGroup.bind();
        int fila = lista_tabla.size()-1;
        jTable1.setRowSelectionInterval(fila, fila);
        jTextField_numcarriles.setEnabled(true);
        jTextField_ruta.setEnabled(true);
        jTextField_tipocarretera.setEnabled(true);
        jTextField_velocidad.setEnabled(true);
        jList_servicio.setEnabled(true);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox_tramoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_tramoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox_tramoActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    int fila = jTable1.getSelectedRow();
        if (fila != -1) {
            String mesg = ")=¿Eliminaras todo sin dejar huella %s?";
            Carretera actual = lista_tabla.get(fila);
            int resp = JOptionPane.showConfirmDialog(this, String.format(mesg,actual), "CONFIRMACION", JOptionPane.YES_NO_OPTION);
            if (resp == JOptionPane.YES_OPTION) {
                Binding bin = bindingGroup.getBinding("tablaEmpleado");
                bin.unbind();
                lista_tabla.remove (actual);
                jTextField_numcarriles.setEnabled(false);
                jTextField_ruta.setEnabled(false);
                jTextField_tipocarretera.setEnabled(false);
                jTextField_velocidad.setEnabled(false);
                jList_servicio.setEnabled(false);
                bin.bind();
    }//GEN-LAST:event_jButton2ActionPerformed
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.acme.entity.Carretera bean;
    private javax.persistence.EntityManager entityManager1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox_tramo;
    private javax.swing.JList jList_servicio;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField_numcarriles;
    private javax.swing.JTextField jTextField_ruta;
    private javax.swing.JTextField jTextField_tipocarretera;
    private javax.swing.JTextField jTextField_velocidad;
    private javax.swing.JToolBar jToolBar1;
    private java.util.List<Tramo> listaTramos;
    private java.util.List<Servicio> lista_servicio;
    private java.util.List<Carretera> lista_tabla;
    private javax.persistence.Query query_servicio;
    private javax.persistence.Query query_tabla;
    private javax.persistence.Query query_tramo;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables


   private final static HashMap<String, String> mapa = new HashMap<String, String>();

    static {
        mapa.put("ruta", "<Ruta>");
        mapa.put("num.carriles", "<Num.carriles>");
        mapa.put("velocidad", "<Velocidad>");
        mapa.put("servicio", "<Servicio>");
        mapa.put("tipo.carretera", "<Tipo.carretera>");
        mapa.put("tramo", "<Tramo>");
    }

    private void establecerMensajes() {
        for (Binding bin : bindingGroup.getBindings()) {
            Object obj = bin.getTargetObject();
            JTextComponent cmp = null;
            if (obj instanceof JTextComponent) {
                cmp = (JTextComponent) obj;
            }
            if (cmp != null) {
                String mensaje = mapa.get(bin.getName());
                PromptSupport.setFontStyle(Font.ITALIC, cmp);
                PromptSupport.setForeground(Color.RED, cmp);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, cmp);
                PromptSupport.setPrompt(mensaje, cmp);
            }
        }
    }


    @Override
    public void stateChanged(ChangeEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}


